﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.ACTP.Components
{
    public partial class Employee
    {
       /// <summary>
       /// 
       /// </summary>
       /// <param name="moduleID"></param>
       /// <param name="projectID"></param>
       /// <param name="certificationID">0: All</param>
       /// <returns></returns>
        public static IList<Employee> SelectCollectionBy_ProjectID_CertificationID_Except_AddedInProject(Guid moduleID, int projectID, int certificationID)
        {
            const string spName = "p_ACTP_Employee_SelectBy_ProjectID_CertificationID_Except_AddedInProject";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, projectID);
            db.AddInParameter(dbCommand, "@CertificationID", SqlDbType.Int, certificationID);

            IDataReader reader = db.ExecuteReader(dbCommand);
            return ConvertToCollection(reader);
        }
        public static IList<Employee> SelectCollectionBy_ProjectID(Guid moduleID, int projectID)
        {
            const string spName = "p_ACTP_Employee_SelectBy_ProjectID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, projectID);

            IDataReader reader = db.ExecuteReader(dbCommand);
            return ConvertToCollection(reader);
        }

        public static Employee GetEmployeeBy_ProjectID_ProjectManager(Guid moduleID, int projectID)
        {
            const string spName = "p_ACTP_Employee_SelectBy_ProjectID_ProjectManager";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@ProjectID", SqlDbType.Int, projectID);

            IDataReader reader = db.ExecuteReader(dbCommand);
            IList<Employee> list = ConvertToCollection(reader);
            if (list.Count > 0)
                return list[0];
            return null;
        }

        public static Employee GetEmployeeBy_MembershipID(int membershipID)
        {
            IList<Employee> collection = SelectCollectionBy_MembershipID(membershipID);
            return collection.Count > 0 ? collection[0] : null;
        }
    }
}
